﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.UserControls;

namespace ERP_RORZE_PERMISSION.Forms
{
    public partial class frm_FunctionEdit : DevExpress.XtraEditors.XtraForm
    {
        public int ID;
        public int FunctionID;
        public string FunctionName;
        public string DepartmentID;
        private LogUtils log;
        private ucFunctions parent;

        public frm_FunctionEdit(ucFunctions fUser)
        {
            InitializeComponent();
            parent = fUser;
            this.CancelButton = this.btCancel;
            log = new LogUtils();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            Functions function = new Functions();
            if (GetData(function))
            {
                string update = "UPDATE [222_Functions] SET [FunctionID] = '" + function.FunctionID +
                                "',[FunctionName] = N'" + function.FunctionName +
                                "',[DepartmentID] = " + function.DepartmentID +
                                "  WHERE [ID]= '" + ID + "'";

                if (BaseDAO.ExecuteNonQuery(update) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.SystemAdmin.Functions;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "FunctionID: " + function.FunctionID + "; FunctionName: " + function.FunctionName;
                    log.WriteLog();

                    if (!Utilities.MessageConfirmationYesNo(PermissionConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(PermissionConstants.Message.UpdateFailed);
                }
                parent.BindDataGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_UserEdit_Load(object sender, EventArgs e)
        {
            BindData();

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        #region Private methods

        private bool CheckDuplicate(string functionId)
        {
            DataTable dt = BaseDAO.ExecuteQuery("Select * from [222_Functions]  where [FunctionID] ='" + functionId + "' AND [FunctionID] <> '" + FunctionID + "'");
            if (dt.Rows != null && dt.Rows.Count > 0)
                return true;
            return false;

        }

        private bool GetData(Functions o)
        {
            #region Check valid
            if (Utilities.IsNullOrEmpty(txtFunctionID.Text.Trim()))
            {
                MessageBox.Show("FunctionID can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtFunctionName.Text.Trim()))
            {
                MessageBox.Show("Function Name can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (CheckDuplicate(txtFunctionID.Text.Trim()))
            {
                MessageBox.Show("FunctionID already exists.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.FunctionID = int.Parse(txtFunctionID.Text);
            o.FunctionName = txtFunctionName.Text;
            o.DepartmentID = 0;

            return true;
        }

        private void BindData()
        {
            txtFunctionID.Text = FunctionID.ToString();
            txtFunctionName.Text = FunctionName;
            cmbDepartment.SelectedValue = DepartmentID;
        }

        #endregion


    }
}